package com.laundry.service.impl;

import com.laundry.dao.NewsDao;
import com.laundry.entity.News;
import com.laundry.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsDao newsDao;
    
    @Override
    public List<News> findAll() {
        return newsDao.findAll();
    }
    
    @Override
    public News findById(Long id) {
        return newsDao.findById(id).orElse(null);
    }
    
    @Override
    public News save(News news) {
        news.setCreateTime(new Date());
        news.setUpdateTime(new Date());
        return newsDao.save(news);
    }
    
    @Override
    public News update(News news) {
        News existNews = newsDao.findById(news.getId()).orElse(null);
        if (existNews == null) {
            return null;
        }
        
        news.setCreateTime(existNews.getCreateTime());
        news.setUpdateTime(new Date());
        return newsDao.save(news);
    }
    
    @Override
    public void deleteById(Long id) {
        newsDao.deleteById(id);
    }
} 